package com.meizi.threalocad.controller;

/**
 * @author Administration
 * @date 2021/5/18 - 14:23 - 星期二
 * @project lambda-stuty
 * @package com.meizi.threalocd.controller
 * @by GGbomd猪猪侠
 */
public class ThreadLocalMyDemo {



    ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private String content;

    public String getContent() {
        //return content;
        // 取出当前线程绑定的变量
        String s = threadLocal.get();
        return s;
    }


    public void setContent(String content) {
        //this.content = content;
        // 将content绑定到当前线程中去
        threadLocal.set(content);
    }

    public static void main(String[] args) {
        ThreadLocalMyDemo demo = new ThreadLocalMyDemo();
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new Runnable(){

                @Override
                public void run() {
                    /**
                     * 每个线程存在一个变量 ， 过一会取出这个变量
                     */
                    demo.setContent(Thread.currentThread().getName() + "的数据");
                    System.out.println("---------------------------------");
                    System.out.println(Thread.currentThread().getName() + "------>" + demo.getContent());
                }
            });
            thread.setName("线程" + i);
            thread.start(); //
        }
    }
}
